<?php
class Users_model extends MY_Model {
	private $module = 'users';
	private $table = 'users';

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`username` LIKE "%'.$this->input->post('content').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`username` LIKE "%'.$this->input->post('content').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->count_all_results(PREFIX.$this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}

	function get_user($username){
		$this->db->select('*');
		$this->db->from(PREFIX.$this->table);
		$this->db->join(PREFIX.'groups', 'cli_groups.id = cli_users.group');
		$this->db->where('username',$username);

		$query = $this->db->get();

		if($query->result()){
			return $query->row();
		}else{
			return false;
		}
	}

	function get_group_user($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.'groups');

		if($query->result()){
			return $query->row();
		}else{
			return false;
		}
	}

	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function saveManagement($fileName=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}

		if($this->input->post('hiddenIdAdmincp')==0){
			//Kiểm tra đã tồn tại chưa?
			$checkData = $this->checkData($this->input->post('userAdmincp'));
			if($checkData){
				print 'error-username-exists';
				exit;
			}

			$checkData = $this->checkEmail($this->input->post('emailAdmincp'));
			if($checkData){
				print 'error-email-exists';
				exit;
			}

			$data = array(
				'username'=> $this->input->post('userAdmincp'),
				'password'=> md5($this->input->post('passAdmincp')),
				'email' => $this->input->post('emailAdmincp'),
				'group'=> $this->input->post('groupAdmincp'),
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			//Kiểm tra đã tồn tại chưa?
			if($result[0]->username!=$this->input->post('userAdmincp')){
				$checkData = $this->checkData($this->input->post('userAdmincp'));
				if($checkData){
					print 'error-username-exists';
					exit;
				}
			}
			if($result[0]->email!=$this->input->post('emailAdmincp')){
				$checkData = $this->checkEmail($this->input->post('emailAdmincp'));
				if($checkData){
					print 'error-email-exists';
					exit;
				}
			}

			if($this->input->post('passAdmincp')==''){
				$pass = $result[0]->password;
			}else{
				$pass = md5($this->input->post('passAdmincp'));
			}
			$data = array(
				'username'=> $this->input->post('userAdmincp'),
				'password'=> $pass,
				'email' => $this->input->post('emailAdmincp'),
				'group'=> $this->input->post('groupAdmincp'),
				'status'=> $status,
				'modified'=> date('Y-m-d H:i:s',time()),
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}

	function get_group(){
		$this->db->select('*');
		$this->db->where('status',1);
		$query = $this->db->get(PREFIX.'groups');
		if($query->result()){
			return $query->result();
		}
		else
			return false;
	}

	function checkData($title){
		$this->db->select('id');
		$this->db->where('username',$title);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}

	function checkEmail($email){
		$this->db->select('id');
		$this->db->where('email',$email);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}

	function checkSlug($slug){
		$this->db->select('id');
		$this->db->where('slug',$slug);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}

	/*----------------------FRONTEND----------------------*/
	function getNews($type){
		$this->db->select('*');
		$this->db->where('type',$type);
		$this->db->where('status',1);
		$this->db->order_by('created','DESC');
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getNewsDetail($slug_detail){
		$this->db->select('id,content');
		$this->db->where('slug',$slug_detail);
		$this->db->where('status',1);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function checkLogin($user){
		$this->db->select('*');
		$this->db->where('username', $user);
		$this->db->where('status', 1);
		$query = $this->db->get(PREFIX.'users');

		foreach ($query->result() as $row){
			$pass = $row->password;
		}

		if(!empty($pass)){
			return $pass;
		}else{
			return false;
		}
	}

	function getAllowDownload($group_id){
		$this->db->select('*');
		$this->db->where('status',1);
		$this->db->where('(`groups` LIKE "%' . $group_id . '%")' );
		$query = $this->db->get(PREFIX.'document');
		if($query->result()){
			foreach ($query->result() as $row) {
				# code...
			}
			return $query->result();
		}
		return false;
	}
	/*--------------------END FRONTEND--------------------*/

	function register(){
		$checkData = $this->checkData($this->input->post('co_username'));
		if($checkData){
			print 'error-username-exists';
			exit;
		}

		$checkData = $this->checkEmail($this->input->post('co_email'));
		if($checkData){
			print 'error-email-exists';
			exit;
		}

		$data = array(
			'username'=> $this->input->post('co_username'),
			'password'=> md5($this->input->post('co_password')),
			'email' => $this->input->post('co_email'),
			'lastname' => $this->input->post('co_name'),
			'phone' => $this->input->post('co_phone'),
			'address'=> $this->input->post('co_address'),
			'group' => 1,
			'status'=> 1,
			'created'=> date('Y-m-d H:i:s',time()),
		);
		if($this->db->insert(PREFIX.$this->table,$data)){
			modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
			return true;
		}

	}
}
